﻿#ifndef TCPCLIENTMANAGER_H
#define TCPCLIENTMANAGER_H
#include <QObject>
#include <QTcpSocket>
#include <QFile>
#include <QDir>

#include "CommonDefine.h"
#include "ChatTools/TransferProxy.h"

class TcpClientManager : public QObject
{
    Q_OBJECT
    QML_PROPERTY_(loginStatus, QString , "");
    QML_PROPERTY_(replyCode, int, -10086)
    QML_PROPERTY_(account,QString, "")
    QML_PROPERTY_(displayIndex,int,0);
    QML_PROPERTY_(transferProxy, TransferProxy*, nullptr)

public:
    enum ReplyCode {
        LoginSucceeded = 0,
        UnKonwError = -10001,
        //密码失败超过一定次数
        PasswordFailedMoreThanTimes = -10002,
        //账号不存在
        AccountNotExist = -10003,
        PasswordError = -10004,
        RegisterAccountExist = -10005,
        RegisterAccountSucceeded = 1,
    };
    //加这个可以在QML使用
    Q_ENUMS(ReplyCode)

    //返回类型
    enum ReplyStatus {
        InfoMessage = 0,
        InfoImage,
        InfoMovie
    };

    TcpClientManager();
    ~TcpClientManager() {
        if(transferProxy) {
            delete transferProxy;
            transferProxy = nullptr;
        }
    }
public slots:
    void        loginAccount(QString account, QString password);
    void        registerAccount(QString account, QString password);
    void        disconnectSlot();
    void        disconectClient();

private slots:
    void        connectSlot();
    void        readyReadSlot();

private:
    void        connectServer();
    void        handleLoginReply(QString replay);
    void        handleRegisterReply(QString replay);
    //获取信息和code
    bool        getInfoAndCode(QString replay, int &code, QString& info);

private:
    QTcpSocket          mTcpSocket;
    bool                mIsConnectFlag     = false;
};

#endif // TCPCLIENTMANAGER_H
